package com.shop.shoppingmall.service.impl;

import com.shop.shoppingmall.dao.TSkuDao;
import com.shop.shoppingmall.dao.TSpecDao;
import com.shop.shoppingmall.entity.TSku;
import com.shop.shoppingmall.service.TSkuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (TSku)表服务实现类
 *
 * @author makejava
 * @since 2020-09-24 10:37:41
 */
@Service("tSkuService")
@Transactional
public class TSkuServiceImpl implements TSkuService {
    @Resource
    private TSkuDao tSkuDao;
    @Resource
    private TSpecDao tSpecDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public TSku queryById(Integer id) {
        return this.tSkuDao.queryById(id);
    }

    /**
     * 新增数据
     *
     * @param tSku 实例对象
     * @return 实例对象
     */
    @Override
    public TSku insert(TSku tSku) {
        this.tSkuDao.insert(tSku);
        return tSku;
    }

    /**
     * 修改数据
     *
     * @param tSku 实例对象
     * @return 实例对象
     */
    @Override
    public TSku update(TSku tSku) {
        this.tSkuDao.update(tSku);
        return this.queryById(tSku.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.tSkuDao.deleteById(id) > 0;
    }


    @Override
    public List<String> findSkuIdListByGoodsId(String goodsId) throws Exception {
        List<TSku> attrList=tSkuDao.findSkuListByGoodsId(goodsId);
        List<String>  skuIds=new ArrayList<>();
        for(TSku t:attrList){
            String[]  specAttrId= t.getSkuId().split(",");
            for (String tt:specAttrId){
                String attrId=tt.split(";")[1];
                if (!skuIds.contains(attrId)){
                    skuIds.add(attrId);
                }
            }
        }
        return skuIds;
    }


    @Override
    public List<TSku> findSkuByGoodsId(String goodsId) throws Exception {
        List<TSku> attrList=tSkuDao.findSkuByGoodsId(goodsId);
        for(TSku t:attrList){
             String[] specAttrId = t.getSkuId().split(",");
                if(specAttrId.length==2){
                    String a  =tSkuDao.selectById("1",t.getId().toString());
                    String a1  = tSkuDao.selectById("4",t.getId().toString());
                    String a2  =tSkuDao.selectById("7",t.getId().toString());
                    String a3  =tSkuDao.selectById("10",t.getId().toString());
                    String sku = a+":"+a1+","+a2+":"+a3;
                    System.out.println(a+":"+a1+","+a2+":"+a3);
                    t.setSkuId(sku);
                }else if(specAttrId.length ==3){
                    String a  =tSkuDao.selectById("1",t.getId().toString());
                    String a1  = tSkuDao.selectById("4",t.getId().toString());
                    String a2  =tSkuDao.selectById("7",t.getId().toString());
                    String a3  =tSkuDao.selectById("10",t.getId().toString());
                    String a4  =tSkuDao.selectById("13",t.getId().toString());
                    String a5  =tSkuDao.selectById("16",t.getId().toString());
                    System.out.println(a+":"+a1+","+a2+":"+a3+","+a4+":"+a5);
                    String sku = a+":"+a1+","+a2+":"+a3+","+a4+":"+a5;
                    t.setSkuId(sku);
            }
        }
        return attrList;
    }

}